package com.seednia.foundation.framework.dao;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.Query;

import com.seednia.foundation.framework.dto.AbstractRecord;
import com.seednia.foundation.framework.dto.JoinParams;
import com.seednia.foundation.framework.dto.QueryParams;
import com.seednia.foundation.framework.service.DataAccessException;
import com.seednia.module.core.project.dto.ProjectDto;

/**
 * This class provides basic operations for all Data Access Object classes
 * 
 * @author Young Suk Ahn
 *
 */
public abstract class AbstractDao implements IDao {

	private String tableName = null;
	
	private List<String> selectProperties = new ArrayList<String>(); // Properties to be loaded in the custom selectJoin
	private String selectColumnsCsv;
	//private Set<String> columnsSet = new LinkedHashSet<String>();
	
	public AbstractDao(String tableName, List<String> selectProperties, List<String> selectColumns)
	{
		this.tableName = tableName;
		
		this.selectProperties = selectProperties;

		if (this.selectProperties == null)
			return;
		StringBuffer buff = new StringBuffer();
		for(String colsName: selectColumns) {
			buff.append("this.").append(colsName).append(", ");
		}
		if (buff.length() > 2)
			buff.delete(buff.length()-2, buff.length()-1);
		this.selectColumnsCsv = buff.toString();
	}
	
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public List<String> getSelectProperties() {
		return selectProperties;
	}

	public String getSelectColumnsCsv() {
		return selectColumnsCsv;
	}

	/**
	 * Sets the createDate values
	 * @param record
	 */
	public void prepForInsert(AbstractRecord record){
		record.setCreateDt(new Date());
		if (record.getUuid() == null || record.getUuid().length() < 2) {
			record.setUuid(generateUuid());
		}
	}
	
	/**
	 * 
	 * @param record
	 */
	public void prepForUpdate(AbstractRecord record){
		record.setLastModifiedDt(new Date());
	}

	/**
	 * Generates a UUID
	 * @return
	 */
	public String generateUuid()
	{
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}
	
}
